package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.AddressData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/10.
 */

public class ShopAddressAdapter extends RecyclerView.Adapter<ShopAddressAdapter.ViewHolder> {

    public static final int TYPE_CHOOSE = 1;
    public static final int TYPE_CHECK = 0;
    public static final int TYPE_MANGER = 2;

    private Context context;
    private OnAddressClickListener listener;
    private List<AddressData> datas;
    private int TYPE;

    public ShopAddressAdapter(Context mContext,List<AddressData> list,OnAddressClickListener clickListener) {
        context = mContext;
        datas = list;
        listener = clickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shop_address, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AddressData address = datas.get(position);
        holder.tvConsigneePerson.setText(address.getReceiverName());
        holder.tvMobile.setText(address.getReceiverPhone());
        holder.tvAddressDetail.setText(address.getProvinceName()+address.getCityName()+address.getAreaName()
                +address.getUnitName()+"-"+address.getDetailAddress());
        if (TYPE == TYPE_CHECK){
            holder.tvDef.setVisibility(View.GONE);
            holder.rlEditBox.setVisibility(View.VISIBLE);
            if ("0".equals(address.getStatus())){
                //没选中
                holder.imgDef.setImageResource(R.mipmap.cart_pressed);
            }else if("1".equals(address.getStatus())){
                holder.imgDef.setImageResource(R.mipmap.cart_selected);
            }
            holder.llSetDefAddress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onSetDef(position);
                }
            });
            holder.llDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onDel(position);
                }
            });
            holder.llEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onEdit(position);
                }
            });
        }else if (TYPE == TYPE_CHOOSE){
            holder.rlEditBox.setVisibility(View.GONE);
            if ("0".equals(address.getStatus())){
                //没选中
                holder.tvDef.setVisibility(View.GONE);
            }else if("1".equals(address.getStatus())){
                holder.tvDef.setVisibility(View.VISIBLE);
            }
            holder.llContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void setTYPE(int TYPE) {
        this.TYPE = TYPE;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_consignee_person)
        TextView tvConsigneePerson;
        @BindView(R.id.tv_mobile)
        TextView tvMobile;
        @BindView(R.id.tv_address_detail)
        TextView tvAddressDetail;
        @BindView(R.id.img_def)
        ImageView imgDef;
        @BindView(R.id.ll_set_def_address)
        LinearLayout llSetDefAddress;
        @BindView(R.id.ll_edit)
        LinearLayout llEdit;
        @BindView(R.id.ll_del)
        LinearLayout llDel;
        @BindView(R.id.tv_def)
        TextView tvDef;
        @BindView(R.id.ll_container)
        LinearLayout llContainer;
        @BindView(R.id.rl_edit_box)
        RelativeLayout rlEditBox;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnAddressClickListener{
        void onEdit(int position);
        void onDel(int position);
        void onSetDef(int position);
        void onItemClick(int position);
    }
}
